package com.colorlight.cloud;

import com.colorlight.cloud.common.utils.HexUtil;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.iot.proto.gateway.Gateway;

import java.io.*;
import java.lang.reflect.Field;

/**
 * @author Demon
 * @date 10/4/2024  下午3:27
 */
public class Util<T> {
	
	public static void printByteArray(byte[] byteArray) {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		for (int i = 0; i < byteArray.length; i++) {
			sb.append(byteArray[i]);
			if (i < byteArray.length - 1) {
				sb.append(", ");
			}
		}
		sb.append("]");
		System.out.println(sb.toString());
	}
	
	public static ByteString getData(byte[] arr) {
		
		Gateway.Msg gatewayMsg = null;
		try {
			gatewayMsg = Gateway.Msg.parseFrom(arr);
			
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
		System.out.println(gatewayMsg);
		return gatewayMsg.getBizContent();
	}
	
	
	
	
	public static void printFields(Field[] fields) {
		for (Field f :fields){
			System.out.println(f.getName());
		}
	}
	public static Gateway.Msg getMsg(String hex) {
		byte[] bytes = HexUtil.fromHex(hex);
		Gateway.Msg msg1 = null;
		try {
			msg1 = Gateway.Msg.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			e.printStackTrace();
		}
		return msg1;
	}

}
